package com.suray.attendance.yida.service.impl;

import com.alibaba.fastjson.JSON;
import com.aliyun.dingtalkyida_1_0.models.*;
import com.aliyun.tea.TeaException;
import com.aliyun.teautil.models.RuntimeOptions;
import com.suray.attendance.common.constant.DingTalkConstant;
import com.suray.attendance.common.dingding.DingTalkClientUtils;
import com.suray.attendance.common.utils.LoggerUtil;
import com.suray.attendance.yida.service.YiDaInstanceService;
import org.springframework.stereotype.Service;
import java.util.*;

/**
 * Description:
 * Author: linyu
 * Date: 2023/9/21
 */
@Service
public class YiDaInstanceServiceImpl implements YiDaInstanceService {



    /**
     * @Description: 根据表单编号和实例id列表查询宜搭的表单的信息
     * @Param: [instanceIdList, accessToken, formId]
     * @Author: jly
     * @Date: 2023/9/21
     * @return
     */
    @Override
    public List<BatchGetFormDataByIdListResponseBody.BatchGetFormDataByIdListResponseBodyResult> getYiDaInstanceInfoListByFromIdAndInstanceIdList(List<String> instanceIdList, String accessToken, String formId) {
        if(instanceIdList==null || instanceIdList.size()<0){
            LoggerUtil.warn("获取宜搭的实例列表数据异常，可能是该表单没有数据记录", YiDaInstanceServiceImpl.class);
            return null;
        }
        com.aliyun.dingtalkyida_1_0.Client client = DingTalkClientUtils.createClientYiDa();
        BatchGetFormDataByIdListHeaders batchGetFormDataByIdListHeaders = new BatchGetFormDataByIdListHeaders();
        batchGetFormDataByIdListHeaders.xAcsDingtalkAccessToken = accessToken;
        BatchGetFormDataByIdListRequest batchGetFormDataByIdListRequest = new BatchGetFormDataByIdListRequest()
                .setFormUuid(formId)
                .setAppType(DingTalkConstant.APPTYPE)
                .setSystemToken(DingTalkConstant.SYSTEMTOKEN)
                .setFormInstanceIdList(
                        instanceIdList
                )
                .setNeedFormInstanceValue(true)
                .setUserId(DingTalkConstant.userId);
        try {
            BatchGetFormDataByIdListResponse response = client.batchGetFormDataByIdListWithOptions(batchGetFormDataByIdListRequest, batchGetFormDataByIdListHeaders, new RuntimeOptions());
            LoggerUtil.info("宜搭信息表单="+ JSON.toJSONString(response.getBody().getResult()), YiDaInstanceServiceImpl.class);
            return response.getBody().getResult();
        } catch (TeaException err) {
            LoggerUtil.error(err.getMessage(), YiDaInstanceServiceImpl.class);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }
        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }

        }
        return null;
    }


    /**
     * @Description: 报工考勤的批量新增到宜搭
     * @Param: [accessToken, formDataJsonList]
     * @return: java.util.List<java.lang.String>
     * @Author: jly
     * @Date: 2023/11/28
     */
    public List<String> batchProductionDataCollectDetailInfoToYiDa(String accessToken,List<String> formDataJsonList) {
        com.aliyun.dingtalkyida_1_0.Client client = DingTalkClientUtils.createClientYiDa();
        BatchSaveFormDataHeaders batchSaveFormDataHeaders = new BatchSaveFormDataHeaders();
        batchSaveFormDataHeaders.xAcsDingtalkAccessToken = accessToken;
        BatchSaveFormDataRequest batchSaveFormDataRequest = new BatchSaveFormDataRequest()
                .setNoExecuteExpression(true)
                .setFormUuid(DingTalkConstant.REPORT_WORK_INFO_FORM)
                .setAppType(DingTalkConstant.APPTYPE)
                .setAsynchronousExecution(false)
                .setSystemToken(DingTalkConstant.SYSTEMTOKEN)
                .setKeepRunningAfterException(true)
                .setUserId(DingTalkConstant.userId)
                .setFormDataJsonList(formDataJsonList);
        try {
            BatchSaveFormDataResponse batchSaveFormDataResponse = client.batchSaveFormDataWithOptions(batchSaveFormDataRequest, batchSaveFormDataHeaders, new RuntimeOptions());
            Integer statusCode = batchSaveFormDataResponse.statusCode;
            if(statusCode==200){
                List<String> reportWorkInstanceIdList = batchSaveFormDataResponse.getBody().getResult();
                if(reportWorkInstanceIdList!=null && reportWorkInstanceIdList.size()>0){
                    return reportWorkInstanceIdList;
                }
            }
        }catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            LoggerUtil.info("创建宜搭报工表单实例失败，异常信息"+err.getMessage(), YiDaInstanceServiceImpl.class);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }
        }
        return null;
    }
}
